<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper	namespace="{{packageName}}.core.{{entity.module}}.mapper.{{entity.codeName}}Mapper">

        <!--通过mybatis将查询结果注入到entity中,通过配置autoMapping="true"由mybatis自动处理映射关系 -->
    <resultMap id="{{entity.codeName}}ResultMap" type="{{packageName}}.core.{{entity.module}}.domain.{{entity.domainClassName}}" autoMapping="true">
		{{#entity.keyField.phisicalDEField}}
		<id property="{{camelCase entity.keyField.codeName}}" column="{{entity.keyField.columnName}}" /><!--主键字段映射-->
		{{/entity.keyField.phisicalDEField}}
		{{#each entity.properties}}
            {{#if fieldMapping}}
		<result property="{{camelCase propertyName}}" column="{{columnName}}"{{#deepStructure}} jdbcType="VARCHAR" typeHandler="{{deepStructureTypeHandler}}"{{/deepStructure}}{{#translator}} jdbcType="VARCHAR" typeHandler="{{translatorTypeHandler}}"{{/translator}}/>
            {{else if nestedMapping}}
        <result property="{{camelCase propertyName}}" column="{{columnName}}" typeHandler="{{packageName}}.core.{{module}}.domain.handlers.{{entityCodeName}}TypeHandler" />
            {{else if referenceMapping}}
        <association property="{{camelCase propertyName}}" javaType="{{packageName}}.core.{{module}}.domain.{{entityDomainClassName}}" column="{{fkField.columnName}}" select="{{packageName}}.core.{{module}}.mapper.{{entityCodeName}}Mapper.selectById" fetchType="lazy"></association>
            {{/if}}
        {{/each}}
    </resultMap>

    <select id="selectById"  resultMap="{{entity.codeName}}ResultMap">
    {{#if entity.viewDataQuery}}
        {{#if entity.keyField.phisicalDEField}}
        <include refid="view" /> 
            {{#if entity.viewDataQuery.where}}
         and 
            {{else}}
         where 
            {{/if}}
        {{else}}
        select t1.* from ( <include refid="view" /> ) t1 
         where 
        {{/if}}
    {{else}}
        select t1.* from {{entity.viewName}} t1 
         where 
        {{#if entity.logicValid}}
            {{#if entity.logicValidField.type.number}}
         t1.{{entity.logicValidField.columnName}} = {{entity.validLogicValue}}
            {{else}}
         t1.{{entity.logicValidField.columnName}} = '{{entity.validLogicValue}}'
            {{/if}}
         and   
        {{/if}}
    {{/if}}
         t1.{{entity.keyField.columnName}}=#{ {{camelCase entity.keyField.codeName}} }
    </select>

    <select id="selectEntity" parameterType="{{packageName}}.core.{{entity.module}}.domain.{{entity.domainClassName}}" resultMap="{{entity.codeName}}ResultMap">
    {{#if entity.viewDataQuery}}
        <include refid="view" /> 
            {{#if entity.viewDataQuery.where}}
         and 
            {{else}}
         where 
            {{/if}}
    {{else}}
        select t1.* from {{entity.viewName}} t1 
         where 
        {{#if entity.logicValid}}
            {{#if entity.logicValidField.type.number}}
         t1.{{entity.logicValidField.columnName}} = {{entity.validLogicValue}}
            {{else}}
         t1.{{entity.logicValidField.columnName}} = '{{entity.validLogicValue}}'
            {{/if}}
         and   
        {{/if}}
    {{/if}}
         ({{#each entity.keyFields as |field|}}{{#unless @first}} and {{/unless}}t1.{{field.columnName}} = #{ {{camelCase field.codeName}} }{{/each}})
    </select>

    <select id="selectEntities" parameterType="java.util.List" resultMap="{{entity.codeName}}ResultMap">
    {{#if entity.viewDataQuery}}
        <include refid="view" /> 
            {{#if entity.viewDataQuery.where}}
         and 
            {{else}}
         where 
            {{/if}}
    {{else}}
        select t1.* from {{entity.viewName}} t1 
         where 
        {{#if entity.logicValid}}
            {{#if entity.logicValidField.type.number}}
         t1.{{entity.logicValidField.columnName}} = {{entity.validLogicValue}}
            {{else}}
         t1.{{entity.logicValidField.columnName}} = '{{entity.validLogicValue}}'
            {{/if}}
         and   
        {{/if}}
    {{/if}}
         ({{#each entity.keyFields as |field|}}{{#unless @first}},{{/unless}}t1.{{field.columnName}}{{/each}})
         in (<foreach collection="list" item="item" index="index" separator=","> ({{#each entity.keyFields as |field|}}{{#unless @first}},{{/unless}}#{ item.{{camelCase field.codeName}} }{{/each}}) </foreach>)
    </select>

    <delete id="deleteEntities" parameterType="java.util.List">
    {{#if entity.logicValid}}
    update {{entity.tableName}} set
            {{#if entity.logicValidField.type.number}}
         {{entity.logicValidField.columnName}} = {{entity.invalidLogicValue}}
            {{else}}
         {{entity.logicValidField.columnName}} = '{{entity.invalidLogicValue}}'
            {{/if}}
    {{else}}
    delete from {{entity.tableName}}
    {{/if}}
    where ({{#each entity.keyFields as |field|}}{{#unless @first}},{{/unless}}{{field.columnName}}{{/each}})
         in (<foreach collection="list" item="item" index="index" separator=","> ({{#each entity.keyFields as |field|}}{{#unless @first}},{{/unless}}#{ item.{{camelCase field.codeName}} }{{/each}}) </foreach>)
    </delete>

    {{#each entity.dataQueries}}
    <sql id="{{camelCase codeName}}" databaseId="{{dsType}}">
        {{dsCode2}}
    </sql>

    {{/each}}
    {{#each entity.dataSets}}
    <select id="search{{pascalCase codeName}}"  parameterType="{{packageName}}.core.{{entity.module}}.filter.{{entity.codeName}}SearchContext"  {{#if enableGroup}}resultType="java.util.HashMap"{{else}}resultMap="{{entity.codeName}}ResultMap"{{/if}}>
        {{select}} 
        from (
            {{#each queries}}
            <include refid="{{camelCase this}}"/>
                {{#unless @last}}
                 union
                {{/unless}}
            {{/each}}
        ) t1 
        <where><if test="ew!=null and ew.sqlSegment!=null and !ew.emptyOfWhere">${ew.sqlSegment}</if></where>
        <if test="ew!=null and ew.sqlSegment!=null and ew.emptyOfWhere">${ew.sqlSegment}</if>
        {{groupBy}}
    </select>


    <select id="list{{pascalCase codeName}}"  parameterType="{{packageName}}.core.{{entity.module}}.filter.{{entity.codeName}}SearchContext"  {{#if enableGroup}}resultType="java.util.HashMap"{{else}}resultMap="{{entity.codeName}}ResultMap"{{/if}}>
        {{select}}
        from (
            {{#each queries}}
            <include refid="{{camelCase this}}"/>
                {{#unless @last}}
                 union
                {{/unless}}
            {{/each}}
        ) t1
        <where><if test="ew!=null and ew.sqlSegment!=null and !ew.emptyOfWhere">${ew.sqlSegment}</if></where>
        <if test="ew!=null and ew.sqlSegment!=null and ew.emptyOfWhere">${ew.sqlSegment}</if>
        {{groupBy}}
    </select>

    {{/each}}
    {{#if entity.viewDataQuery}}
    <select id="list{{pascalCase entity.viewDataQuery.codeName}}"  parameterType="{{packageName}}.core.{{entity.module}}.filter.{{entity.codeName}}SearchContext"  {{#if enableGroup}}resultType="java.util.HashMap"{{else}}resultMap="{{entity.codeName}}ResultMap"{{/if}}>
        select t1.*
        from (
            <include refid="view" />
        ) t1
        <where><if test="ew!=null and ew.sqlSegment!=null and !ew.emptyOfWhere">${ew.sqlSegment}</if></where>
        <if test="ew!=null and ew.sqlSegment!=null and ew.emptyOfWhere">${ew.sqlSegment}</if>
    </select>

    {{/if}}
    {{#each entity.references as | reference |}}
    <select id="findBy{{pascalCase reference.fkField.codeName}}" resultMap="{{entity.codeName}}ResultMap">
        {{#if reference.nestedDataSet}}
        {{reference.nestedDataSet.select}} from (
            {{#each reference.nestedDataSet.queries as |qa|}}
            <include refid="{{camelCase qa}}"/>
                {{#unless @last}}
                 union
                {{/unless}}
            {{/each}}
        ) t1
        {{else}}
        select t1.* from {{#if entity.viewName}}{{entity.viewName}}{{else}}{{entity.tableName}}{{/if}} t1
        {{/if}}
        where {{reference.fkField.columnName}} in <foreach item="id" index="index" collection="{{camelCase reference.fkField.codeName}}s" open="(" separator="," close=")">#{id}</foreach> {{reference.nestedDataSet.orderBy}}
    </select>

    {{/each}}

</mapper>